package ggze.GGZEServer.home.service.impl;

import ggze.GGZEServer.home.dao.inter.SoBlanceDao;
import ggze.GGZEServer.home.dao.inter.SoConsumeRecordDao;
import ggze.GGZEServer.home.dao.inter.SoExpendRecordDao;
import ggze.GGZEServer.home.entity.SoBlance;
import ggze.GGZEServer.home.entity.SoConsumeRecord;
import ggze.GGZEServer.home.entity.SoExpendRecord;
import ggze.GGZEServer.home.enums.BlanceEnum;
import ggze.GGZEServer.home.service.ExpendService;
import ggze.actuator.ActuatorController;
import ggze.tool.TimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ExpendServiceImpl implements ExpendService {
    @Autowired
    private ActuatorController actuatorController;
    @Autowired
    private SoBlanceDao soBlanceDao;
    @Autowired
    private SoExpendRecordDao soExpendRecordDao;

    @Override
    public boolean changeExpend(SoBlance soBlance, String type) throws Exception {
        SoBlance soUser = soBlanceDao.isSelect("f_user_id", soBlance.getF_USER_ID());
        if (soBlance.getF_EXPEND_JE() != 0) {
            soUser.setF_EXPEND_JE(soUser.getF_EXPEND_JE() + soBlance.getF_EXPEND_JE());
        }
        actuatorController.execute(soUser, "update", soBlanceDao);
        //流水
        SoExpendRecord soExpendRecord = new SoExpendRecord();
        String time = TimeUtils.getTimeFormat();
        soExpendRecord.setF_CREATE_TIME(time);
        soExpendRecord.setF_METHOD_TIME(time);
        soExpendRecord.setF_NOTE(BlanceEnum.EXPEND.getLables(type));
        soExpendRecord.setF_SELES1(type);
        soExpendRecord.setF_USER_NAME(soUser.getF_USER_NAME());
        soExpendRecord.setF_USRE_ID(soUser.getF_USER_ID());
        soExpendRecord.setF_MONEY(soBlance.getF_EXPEND_JE());
        //不加入匹配
        if(soExpendRecord.getF_SELES1().equals("0")){
            soExpendRecord.setF_SELES2("1");
        }
        if(soExpendRecord.getF_SELES1().equals("1")){
            soExpendRecord.setF_SELES2("0");
        }
        //付款状态
        soExpendRecord.setF_SELES3("0");
        //确认状态
        soExpendRecord.setF_SELES4("0");
        actuatorController.execute(soExpendRecord, "add", soExpendRecordDao);
        return true;
    }
}
